package Day07;

import java.util.Scanner;

public class ATM {
//  简化ATM机程序 登录相应账号进行存取转账，且账号密码错误三次 退出系统
    static String [][] users = {
        {"aaaaaa","111111","1000"},
        {"bbbbbb","222222","2000"},
        {"cccccc","333333","3000"},
        {"dddddd","444444","4000"},
        {"eeeeee","555555","5000"}
    };
    static Scanner scan = new Scanner(System.in);
    static int index = 0;
//    控制atm机流程的方法
    public static void process(){
        System.out.println("欢迎使用xx银行");
        while (true){
            login();
            menu();
        }
    }
//    登录方法
    public static void login(){
        for (int i = 0; i <= 3;i++){
            System.out.println("请输入账号: ");
            String account = scan.next();
            System.out.println("请输入密码: ");
            String pwd = scan.next();
            for (int j = 0;j < users.length;j++){
                if (users[j][0].equals(account) && users[j][1].equals(pwd)){
                    System.out.println("登录成功");
                    index = j;
                    return;
                }
            }
            System.out.println("登录失败，请重新输入账号与密码,还剩" + (2 - i) + "次输入机会");
        }
        System.out.println("输入错误次数已达上限，卡已冻结");
        System.exit(0);
    }
//    菜单界面方法
    public static void menu(){
        while (true){
            System.out.println("请选择操作：1-查询 2-存款 3-取款 4-转账 5-切换账号 6-更改密码 7-退出");
            int input = scan.nextInt();
            if (input == 1){
                search();
            }else if (input == 2){
                save();
            }else if (input == 3){
                get();
            }else if (input == 4){
                transfer();
            }else if (input == 5){
                System.out.println("账号已登出");
                return;
            }else if(input == 6){
                change();
            } else if (input == 7){
                System.out.println("谢谢使用，再见！");
                System.exit(0);
            }else {
                System.out.println("输入错误,请重新选择");
            }
        }
    }
//    存款方法
    public static void save(){
        System.out.println("请输入存储金额");
        double money = scan.nextDouble();
        users[index][2] = Double.parseDouble(users[index][2]) + money + "";
        System.out.println("存储成功，当前余额为 " +users[index][2]);
    }
//    取款方法
    public static void get(){
        System.out.println("请输入取款金额");
        double money = scan.nextDouble();
        if (Double.parseDouble(users[index][2]) > money){
            users[index][2] = Double.parseDouble(users[index][2]) - money + "";
            System.out.println("取款成功，当前余额为 " + users[index][2]);
        }else{
            System.out.println("当前余额不足，取款失败");
        }
    }
//    查询方法
    public static void search(){
        System.out.println("您的账户余额为 " + users[index][2]);
    }
//    转账方法
    public static void transfer() {
        System.out.println("请输入对方账号");
        String account = scan.next();
        if (users[index][0].equals(account)) {
            System.out.println("不能给自己转账，请重新选择");
            return;
        }
        for (int j = 0; j < users.length; j++) {
            if (users[j][0].equals(account)) {
                System.out.println("请输入转账金额");
                double money = scan.nextDouble();
                for (int k = 0; k < users.length; k++) {
                    if (Double.parseDouble(users[index][2]) < money) {
                        System.out.println("对不起，您的余额不足，无法转账");
                        return;
                    } else {
                        users[index][2] = Double.parseDouble(users[index][2]) - money + "";
                        users[j][2] = Double.parseDouble(users[j][2]) + money + "";
                        System.out.println("转账成功，当前余额为 " + users[index][2]);
                        return;
                    }
                }
            }
        }
        System.out.println("输入账号有误，请重新转账");
    }
//    更改密码的方法
    public static void change(){
        System.out.println("请输入原密码");
        String pwd = scan.next();
        if (users[index][1].equals(pwd)){
            System.out.println("请输入新密码");
            String newpwd1 = scan.next();
            System.out.println("请再次输入新密码");
            String newpwd2 = scan.next();
            if (newpwd1.equals(newpwd2)){
                users[index][1] = newpwd1;
                System.out.println("密码修改成功请重新登录");
                login();
            }else {
                System.out.println("两次新密码输入不相同，请重新输入");
                return;
            }
        }else{
            System.out.println("密码输入有误");
            return;
        }
    }
    public static void main(String[] args) {
        process();
    }
}
